package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.LoginUserData;
import com.cskaoyan.bean.WxLoginUserData;
import com.cskaoyan.bean.WxUserInfoBean;
import com.cskaoyan.bean.vo.promotion_vo.BaseRespWithoutDataVO;
import com.cskaoyan.bean.wx_bo.RegCaptchaWxAuthBO;
import com.cskaoyan.bean.wx_bo.RegisterWxAuthBO;
import com.cskaoyan.config.MallToken;
import com.cskaoyan.service.SendSms;
import com.cskaoyan.service.WxUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @USER: cuiweiguo#
 * @DATE: 2022/5/8
 * @TIME: 9:59
 * @PROJECT_NAME: mallproject2
 */
@RestController
@RequestMapping("wx/auth")
@CrossOrigin
public class WxAuthController {
    @Autowired
    WxUserService wxUserService;

    @Autowired
    SendSms sendSms;

    @RequestMapping("login")
    public BaseRespVo<WxLoginUserData> login(@RequestBody Map map) {
        //http://localhost:8083/wx/auth/login
        //请求参数{"username":"admin123","password":"admin123"}
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        //查询数据库--因为密码加密了，所以现在没法做查询
        //int code = authService.queryAdminByUsernameAndPassword(username,password);
        Subject subject = SecurityUtils.getSubject();
        MallToken wxToken = new MallToken(username, password, "wx");
        subject.login(wxToken);
        // subject.login(new UsernamePasswordToken(username, password));
        if (subject.isAuthenticated()) {
            Integer userId = (Integer) subject.getPrincipals().getPrimaryPrincipal();
            //通过id查询用户信息
            WxUserInfoBean userInfo = wxUserService.queryUserInfoByUserId(userId);
            WxLoginUserData loginUserData = new WxLoginUserData();

            loginUserData.setUserInfo(userInfo);
            //loginUserData.setToken("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0aGlzIGlzIG1hcmtldCB0b2tlbiIsImF1ZCI6Ik1JTklBUFAiLCJpc3MiOiJNQVJLRVQiLCJleHAiOjE2NTE5ODE5ODcsInVzZXJJZCI6MSwiaWF0IjoxNjUxOTc0Nzg3fQ.kuAZt6hJIaD5Qm1LgJO6fVT0eHz1UyzmmL8DFSOIvJ0");
            //Serializable id = subject.getSession().getId();
            Session session = subject.getSession();
            session.setAttribute("userId",userId);
            loginUserData.setToken((String) subject.getSession().getId());
            return BaseRespVo.ok(loginUserData);

        } else {
            return null;
        }
    }

    //TODO 短信存在于缓存的时间如何解决？redis？
    @RequestMapping("regCaptcha")
    public BaseRespWithoutDataVO regCaptcha(@RequestBody @Validated RegCaptchaWxAuthBO regCaptchaWxAuthBO
            , BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BaseRespWithoutDataVO.fail(402, "参数值不对");
        }
        return BaseRespWithoutDataVO.ok();
    }

//    @RequestMapping("register")
//    public Object register(@RequestBody @Validated RegisterWxAuthBO registerWxAuthBO
//            , BindingResult bindingResult) {
//    }


    @PostMapping("logout")
    public BaseRespVo<WxLoginUserData> logout(@RequestBody Map map) {
        return BaseRespVo.ok("null");
    }

}
